package gui;

import io.ItemAgenda;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import logica.ActualizacionCalendarioListener;
import logica.Nucleo;

/**
 *
 * @author Mariano Zapata
 */
public class DialogoAgenda extends javax.swing.JPanel implements ActualizacionCalendarioListener {

    private DefaultTableModel modeloTabla;
    private DefaultListModel modeloLista;
    private Nucleo nucleo;
    private String retornoExcepcion;
    private DialogoAgendaNota dan;
    private List<Integer> diasImportantes;
    
    private static final int [] CANTIDAD_DIAS_MES= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    /** Creates new form DialogoAgenda */
    public DialogoAgenda(Nucleo nucleo) throws RemoteException
    {
        initComponents();
        Image icono= new ImageIcon(getClass().getResource("/gui/icono/logo_chico.png")).getImage();
        dialogo.setIconImage(icono);
        dialogo.getContentPane().setLayout(new BorderLayout());
        modeloTabla= (DefaultTableModel) tabla.getModel();
        RenderTablaAgenda renderTabla= new RenderTablaAgenda(this);
        tabla.setDefaultRenderer(String.class, renderTabla);
        modeloLista= new DefaultListModel();
        RenderListaAgenda renderLista= new RenderListaAgenda();
        lista.setModel(modeloLista);
        lista.setCellRenderer(renderLista);
        this.nucleo= nucleo;
        retornoExcepcion= "";
        dan= null;
        GregorianCalendar cal= new GregorianCalendar();
        selectorAnio.getModel().setValue(cal.get(GregorianCalendar.YEAR));
        selectorMes.setSelectedIndex(cal.get(GregorianCalendar.MONTH));
        this.cargarTabla(cal.get(GregorianCalendar.YEAR), cal.get(GregorianCalendar.MONTH), cal.get(GregorianCalendar.DATE));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogo = new javax.swing.JDialog();
        jPanel1 = new javax.swing.JPanel();
        selectorAnio = new javax.swing.JSpinner();
        selectorMes = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        eliminar = new javax.swing.JButton();
        nuevo = new javax.swing.JButton();
        modificar = new javax.swing.JButton();

        dialogo.setTitle("Agenda");
        dialogo.setModal(true);
        dialogo.setResizable(false);

        javax.swing.GroupLayout dialogoLayout = new javax.swing.GroupLayout(dialogo.getContentPane());
        dialogo.getContentPane().setLayout(dialogoLayout);
        dialogoLayout.setHorizontalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogoLayout.setVerticalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Calendario"));

        selectorAnio.setModel(new javax.swing.SpinnerNumberModel(2011, 2011, 2100, 1));
        selectorAnio.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cambioAnio(evt);
            }
        });

        selectorMes.setMaximumRowCount(12);
        selectorMes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Noviembre", "Diciembre" }));
        selectorMes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cambioMes(evt);
            }
        });

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setCellSelectionEnabled(true);
        tabla.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabla.getTableHeader().setResizingAllowed(false);
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cambioDiaMouse(evt);
            }
        });
        tabla.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cambioDiaKey(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(selectorAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(selectorMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectorAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectorMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Agenda"));

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        lista.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lista.setEnabled(false);
        lista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seleccionoItemMouse(evt);
            }
        });
        lista.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                seleccionoItemKey(evt);
            }
        });
        jScrollPane2.setViewportView(lista);

        eliminar.setText("Eliminar");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminar(evt);
            }
        });

        nuevo.setText("Nuevo");
        nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevo(evt);
            }
        });

        modificar.setText("Ver/Modificar");
        modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificar(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(nuevo)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(modificar)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(eliminar)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nuevo)
                    .addComponent(modificar)
                    .addComponent(eliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cambioAnio(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cambioAnio
        try {
            this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), 0);
        } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                      dialogo.setVisible(false);}
    }//GEN-LAST:event_cambioAnio

    private void cambioMes(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cambioMes
        try {
            this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), 0);
        } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                      dialogo.setVisible(false);}
    }//GEN-LAST:event_cambioMes

    private void cambioDiaKey(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cambioDiaKey
        int tecla= evt.getKeyCode();
        if (tecla == KeyEvent.VK_RIGHT || tecla == KeyEvent.VK_LEFT || tecla == KeyEvent.VK_DOWN ||
                tecla == KeyEvent.VK_UP)
            this.cambioDiaMouse(null);
    }//GEN-LAST:event_cambioDiaKey

    private void cambioDiaMouse(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cambioDiaMouse
        String f= this.obtenerFechaSeleccionada();
        if (f.isEmpty())
        {
            modeloLista.clear();
            this.habilitarZonaAgenda(false);
        }
        else
            this.cargarLista(f);
    }//GEN-LAST:event_cambioDiaMouse

    private void eliminar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminar
        ItemAgenda i= (ItemAgenda) lista.getSelectedValue();
        if (i != null)
        {
            int resp= JOptionPane.showConfirmDialog(this, "¿Seguro desea eliminar este recordatorio?", "Eliminar",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (resp == JOptionPane.YES_OPTION)
            {
                try {
                    this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
                    nucleo.eliminarItemAgenda(i);
                    modeloLista.removeElement(i);
                    int fila= tabla.getSelectedRow();
                    int columna= tabla.getSelectedColumn();
                    int dia= Integer.parseInt((String) modeloTabla.getValueAt(fila, columna));
                    this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), dia);
                    this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    JOptionPane.showMessageDialog(this, "El recordatorio ha sido eliminado.", "Eliminar",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                              this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                                              dialogo.setVisible(false);}
            }
        }
    }//GEN-LAST:event_eliminar

    private void nuevo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevo
        ItemAgenda i= new ItemAgenda(-1);
        i.setFecha(this.obtenerFechaSeleccionada());
        dan= new DialogoAgendaNota(i);
        i= dan.hacerVisible(this);
        dan= null;
        if (i != null)
        {
            try {
                this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
                List<ItemAgenda> items= nucleo.crearItemAgenda(i);
                this.cargarLista(items);
                int fila= tabla.getSelectedRow();
                int columna= tabla.getSelectedColumn();
                int dia= Integer.parseInt((String) modeloTabla.getValueAt(fila, columna));
                this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), dia);
                this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                JOptionPane.showMessageDialog(this, "El recordatorio ha sido creado.", "Nuevo",
                        JOptionPane.INFORMATION_MESSAGE);
            } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                          this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                                          dialogo.setVisible(false);}
        }
    }//GEN-LAST:event_nuevo

    private void modificar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificar
        ItemAgenda i= (ItemAgenda) lista.getSelectedValue();
        if (i != null)
        {
            dan= new DialogoAgendaNota(i);
            i= dan.hacerVisible(this);
            dan= null;
            if (i != null)
            {
                try {
                    this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
                    List<ItemAgenda> items= nucleo.modificarItemAgenda(i);
                    this.cargarLista(items);
                    int fila= tabla.getSelectedRow();
                    int columna= tabla.getSelectedColumn();
                    int dia= Integer.parseInt((String) modeloTabla.getValueAt(fila, columna));
                    this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), dia);
                    this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    JOptionPane.showMessageDialog(this, "El recordatorio ha sido modificado.", "Modificar",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                              this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                                              dialogo.setVisible(false);}
            }
        }
    }//GEN-LAST:event_modificar

    private void seleccionoItemKey(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_seleccionoItemKey
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
            this.modificar(null);
    }//GEN-LAST:event_seleccionoItemKey

    private void seleccionoItemMouse(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seleccionoItemMouse
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1 && evt.getClickCount() == 2)
            this.modificar(null);
    }//GEN-LAST:event_seleccionoItemMouse

    public String hacerVisible(Component c)
    {
        nucleo.agregarActualizacionCalendarioListener(this);
        dialogo.getContentPane().add(this, BorderLayout.CENTER);
        dialogo.pack();
        dialogo.setLocationRelativeTo(c);
        dialogo.setVisible(true);
        dialogo.dispose();
        nucleo.quitarActualizacionCalendarioListener();
        return retornoExcepcion;
    }

    public List<Integer> getDiasImportantes() {
        return diasImportantes;
    }

    private void actualizarDiasImportantes(int anio, int mes) throws RemoteException
    {
        GregorianCalendar cal= new GregorianCalendar();
        int cantidadDias= CANTIDAD_DIAS_MES[mes];
        if (mes == 1 && cal.isLeapYear(anio))
            cantidadDias= 29;
        String fechaI= "1/" + (mes + 1) + "/" + anio;
        String fechaF= cantidadDias + "/" + (mes + 1) + "/" + anio;
        diasImportantes= nucleo.obtenerDiasConEventosImportantes(fechaI, fechaF);
    }
    
    // diaSeleccionar= 0 para no seleccionar día. Mes va desde 0 a 11.
    private void cargarTabla(int anio, int mes, int diaSeleccionar) throws RemoteException
    {
        GregorianCalendar cal= new GregorianCalendar(anio, mes, 1);
        int columna= cal.get(Calendar.DAY_OF_WEEK) - 1;
        int cantidadDias= CANTIDAD_DIAS_MES[mes];
        modeloLista.clear();
        this.habilitarZonaAgenda(false);
        if (mes == 1 && cal.isLeapYear(anio))
            cantidadDias= 29;
        tabla.clearSelection();
        for (int f= 0; f < 6; f++)
        {
            for (int c= 0; c < 7; c++)
                modeloTabla.setValueAt("", f, c);
        }
        this.actualizarDiasImportantes(anio, mes);
        int fila= 0;
        for (int actual= 1; actual <= cantidadDias; actual++)
        {
            modeloTabla.setValueAt(String.valueOf(actual), fila, columna);
            if (actual == diaSeleccionar)
                tabla.changeSelection(fila, columna, false, false);
            if (columna == 6)
            {
                fila+= 1;
                columna= 0;
            }
            else
                columna+=1;
        }
        if (diaSeleccionar > 0)
            this.cargarLista(diaSeleccionar + "/" + (mes + 1) + "/" + anio);
    }
    
    private void cargarLista(String fecha)
    {
        try {
            List<ItemAgenda> items= nucleo.obtenerItemsAgenda(fecha);
            modeloLista.clear();
            this.habilitarZonaAgenda(true);
            for (ItemAgenda i : items)
                modeloLista.addElement(i);
        } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                      dialogo.setVisible(false);}
    }
    
    private void cargarLista(List<ItemAgenda> items)
    {
        int indice= lista.getSelectedIndex();
        modeloLista.clear();
        for (ItemAgenda i : items)
            modeloLista.addElement(i);
        if (indice >= 0)
            lista.setSelectedIndex(indice);
    }
    
    private void habilitarZonaAgenda(boolean habilitar)
    {
        eliminar.setEnabled(habilitar);
        modificar.setEnabled(habilitar);
        nuevo.setEnabled(habilitar);
        lista.setEnabled(habilitar);
    }
    
    private void habilitarZonaCalendario(boolean habilitar)
    {
        selectorAnio.setEnabled(habilitar);
        selectorMes.setEnabled(habilitar);
        tabla.setEnabled(habilitar);
    }
    
    private String obtenerFechaSeleccionada()
    {
        int fila= tabla.getSelectedRow();
        int columna= tabla.getSelectedColumn();
        String fech= "";
        if (fila >= 0 && columna >= 0)
        {
            String dia= (String) modeloTabla.getValueAt(fila, columna);
            if (!dia.isEmpty())
                fech= dia + "/" + (selectorMes.getSelectedIndex() + 1) + "/" + selectorAnio.getValue();
        }
        return fech;
    }

    private boolean mismoAnioYMes(String f1, String f2)
    {
        if (f1.isEmpty() || f2.isEmpty())
            return false;
        String [] partes1= f1.split("/");
        String [] partes2= f2.split("/");
        int anio1= Integer.parseInt(partes1[2]);
        int mes1= Integer.parseInt(partes1[1]);
        int anio2= Integer.parseInt(partes2[2]);
        int mes2= Integer.parseInt(partes2[1]);
        return (anio1 == anio2 && mes1 == mes2);
    }
    
    @Override
    public void actualizarListaAgenda(List<ItemAgenda> l) {
        ItemAgenda i= l.get(0); //ESTA LISTA SIEMPRE TIENE AL MENOS UN ELEMENTO.
        this.habilitarZonaCalendario(false);
        String fechaSeleccionada= this.obtenerFechaSeleccionada();
        if (i.getFecha().equals(fechaSeleccionada))
        {
            this.habilitarZonaAgenda(false);
            this.cargarLista(l);
            this.habilitarZonaAgenda(true);
        }
        if (this.mismoAnioYMes(i.getFecha(), fechaSeleccionada))
        {
            try {
                int fila= tabla.getSelectedRow();
                int columna= tabla.getSelectedColumn();
                int dia= Integer.parseInt((String) modeloTabla.getValueAt(fila, columna));
                this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), dia);
            } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                          dialogo.setVisible(false);}
        }
        this.habilitarZonaCalendario(true);
    }

    @Override
    public void quitarDeListaAgenda(ItemAgenda i) {
        this.habilitarZonaCalendario(false);
        String fechaSeleccionada= this.obtenerFechaSeleccionada();
        if (i.getFecha().equals(fechaSeleccionada))
        {
            this.habilitarZonaAgenda(false);
            if (dan != null && dan.getItemAbierto().equals(i))
            {
                dan.forzarCierre();
                dan= null;
            }
            modeloLista.removeElement(i);
            this.habilitarZonaAgenda(true);
        }
        if (this.mismoAnioYMes(i.getFecha(), fechaSeleccionada))
        {
            try {
                int fila= tabla.getSelectedRow();
                int columna= tabla.getSelectedColumn();
                int dia= Integer.parseInt((String) modeloTabla.getValueAt(fila, columna));
                this.cargarTabla((Integer) selectorAnio.getValue(), selectorMes.getSelectedIndex(), dia);
            } catch (RemoteException ex) {retornoExcepcion= ex.getMessage();
                                          dialogo.setVisible(false);}
        }
        this.habilitarZonaCalendario(true);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog dialogo;
    private javax.swing.JButton eliminar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lista;
    private javax.swing.JButton modificar;
    private javax.swing.JButton nuevo;
    private javax.swing.JSpinner selectorAnio;
    private javax.swing.JComboBox selectorMes;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables
}
